
<template>
  <div id="card_write_off">
		<magic-header v-if="!isWxPublic" titleName="卡券核销"></magic-header>
    <section>
      <div class="qrcode" @click="scan">
        <i class="iconfont icon-mingpianxuanzhong05"></i>
        <p>扫一扫</p>
      </div>
      <p>对准卡券二维码扫码即可进行核销</p>
    </section>
    <section>
      <div class="inputHand">
        <input @click="go('card_write_off_input')" type="button" value="手动输入">
      </div>
    </section>
  </div>
</template>
<script>
import magicHeader from "components/magicHeader";
import cardWriteOff from "services/vfhz/card_write_off";
import wx from "weixin-js-sdk";
export default {
  name: "card_write_off",
  data() {
    return {
      userId: "",
      shopId: "",
      isAdmin: false,
      isWxPublic: false
    };
  },
  created() {
    alert(this.$.env);
    this.userId = this.$.userId;
    this.shopId = this.$.shopId;
    alert(this.userId);
    alert(this.shopId);
    if (this.$.env === "wxPublic") {
      this.getSign();
      this.isWxPublic = true;
      if (this.userId) {
        cardWriteOff.bind(this)(
          {
            methodName: "QueryRoleHasAuthority",
            shopId: this.shopId,
            userId: this.userId,
            keywords: ["admin:activity"]
          },
          res => {
            alert(res);
            console.log(res);
            this.isAdmin = res.isAdmin;
          },
          err => {
            this.$.toast("您的帐号暂无权限使用卡券核销功能");
            console.log(err);
          }
        );
      }
    } else {
      if (this.userId) {
        cardWriteOff.bind(this)(
          {
            methodName: "QueryRoleHasAuthority",
            shopId: this.shopId,
            userId: this.userId,
            keywords: ["admin:activity"]
          },
          res => {
            alert(res);
            console.log(res);
            this.isAdmin = res.isAdmin;
          },
          err => {
            console.log(err);
            this.$.toast("您的帐号暂无权限使用卡券核销功能");
          }
        );
      }
    }
  },
  methods: {
    go(params) {
      if (this.userId) {
        if (this.isAdmin === false) {
          this.$.toast("您的账号暂无权限使用卡券核销功能");
          return;
        }
        this.$router.push({
          name: params
        });
      } else {
        this.$.goLogin({
          name: "login"
        });
      }
    },
    scan() {
      if (this.userId) {
        if (this.isAdmin === false) {
          this.$.toast("您的帐号暂无权限使用卡券核销功能");
          return;
        }
        let vm = this;
        if (this.isWxPublic) {
          wx.scanQRCode({
            needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
            scanType: ["qrCode", "barCode"], // 可以指定扫二维码还是一维码，默认二者都有
            success: function(res) {
              alert(res);
              let result = res.resultStr;
              let code = result.split("=")[2];
              alert(code);
              cardWriteOff.bind(vm)(
                {
                  methodName: "QueryCouponByVerification",
                  shopId: vm.shopId,
                  userId: vm.userId,
                  code: "",
                  couponCodeId: code
                },
                res => {
                  alert(JSON.stringify(res));
                  console.log(res);
                  alert(JSON.stringify(res));
                  vm.$router.push({
                    name: "card_write_off_detail",
                    params: {
                      name: res.name,
                      shopName: res.shopName,
                      state: res.state,
                      img: vm.addPath(res.img),
                      endDate: res.endDate,
                      code: res.couponCodeId,
                      type:res.type
                    }
                  });
                },
                err => {
                  switch (err) {
                    case "卡券不支持扫码核销":
                      vm.$.toast("该券仅限V商在线消费时使用，请告知用户");
                      break;
                    case "卡券所属店铺和管理员所属店铺不一致":
                      vm.$.toast("该卡券非贵店铺发放");
                      break;
                    case "卡券已失效":
                      vm.$.toast("该券已失效");
                      break;
                  }
                }
              );
            }
          });
        } else {
          cardWriteOff.bind(vm)(
            {
              methodName: "QueryCouponByVerification",
              shopId: vm.shopId,
              userId: vm.userId,
              code: "",
              couponCodeId: 33037
            },
            res => {
              console.log(res);
              this.$router.push({
                name: "card_write_off_detail",
                params: {
                  name: res.name,
                  shopName: res.shopName,
                  state: res.state,
                  img: vm.addPath(res.img),
                  endDate: res.endDate,
                  code: res.couponCodeId,
                  type:res.type
                }
              });
            },
            err => {
              setTimeout(() => {
                this.$.toast(err);
              }, 500);
              // this.$.toast(err);
              // console.log(err);
            }
          );
          this.$.push(
            "router_scan",
            {},
            res => {
              alert(res);
              var code = res;
              cardWriteOff.bind(vm)(
                {
                  methodName: "QueryCouponByVerification",
                  shopId: vm.shopId,
                  userId: vm.userId,
                  code: code
                },
                res => {
                  console.log(res);
                },
                err => {
                  this.$.toast(err);
                  console.log(err);
                }
              );
            },
            err => {}
          );
        }
      } else {
        this.$.goLogin({
          name: "login"
        });
      }
    },
    getSign() {
      alert("签名1");
      let signHref = location.href.split("#")[0];
      let signOpenId = this.$.state;
      let signUrlInfo = encodeURIComponent(signHref);
      alert("签名2");
      let signData = {
        clientType: '3',
        methodName: "QueryJssdkConfig",
        shopId: "",
        userId: "",
        urlInfo: signUrlInfo,
        wechatOpenId: signOpenId
      };
      alert(JSON.stringify(signData));
      this.$.wxGet(signData).then(res => {
        var res = res.data;
        alert(JSON.stringify(res.data));
        wx.config({
          debug: true,
          appId: res.appId,
          timestamp: res.timestamp,
          nonceStr: res.nonceStr,
          signature: res.signature,
          jsApiList: ["scanQRCode"]
        });
      });
    }
  },
  components: {
    magicHeader
  }
};
</script>
<style scoped="scoped" lang="scss">
#card_write_off {
  text-align: center;
  font-size: 14px;
  .qrcode {
    width: 200px;
    height: 200px;
    margin: 20px auto;
    border-radius: 50%;
    background-color: #fb8713;
    color: #fff;
    i {
      display: inline-block;
      color:#fff;
      font-size: 100px;
      margin-top: 40px;
    }
    p {
      margin-top: 10px;
      font-size: 16px;
    }
  }
  .inputHand {
    margin-top: 30px;
    input {
      width: 100px;
      height: 36px;
      line-height: 36px;
      border-radius: 5px;
      border: none;
      background-color: #fb8713;
      color: #fff;
      font-size: 14px;
    }
  }
}
</style>